//
//  ForgetPassword.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/29.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "ForgetPassword.h"
#define timeLine 60

@interface ForgetPassword () {
    BOOL IsSendCode;
    NSTimer *timer;
    NSInteger timerCount;
}

@property (weak, nonatomic) IBOutlet UILabel *telNumber;

@property (weak, nonatomic) IBOutlet UILabel *telCodes;

@end

@implementation ForgetPassword

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_COLOR;
    _resetPwdLab.adjustsFontSizeToFitWidth = YES;
    _telNumber.adjustsFontSizeToFitWidth = YES;
    _telCodes.adjustsFontSizeToFitWidth = YES;
    
    [self leftButton:nil];
    IsSendCode = NO;
    timerCount = timeLine;
    _telNum.text = _telStr;
    _sendCodeBtn.layer.borderWidth = 1;
    _sendCodeBtn.layer.borderColor = BLUE_COLOR.CGColor;
    _sendCodeBtn.layer.masksToBounds = YES;
    _sendCodeBtn.layer.cornerRadius = BTN_RADIUS;
    [_sendCodeBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sendCode)]];
    
    _submitBtn.layer.masksToBounds = YES;
    _submitBtn.layer.cornerRadius = BTN_RADIUS;
    
    switch ([_sms_type integerValue]) {
        case 2:_resetPwdLab.text = @"新登录密码";_recentPwd.keyboardType = UIKeyboardTypeDefault;break;
        case 3:_resetPwdLab.text = @"新交易密码";_recentPwd.keyboardType = UIKeyboardTypeNumberPad;break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)sendCode {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/getMobileVerifyCode";
    request.params = @{@"mobile":_telStr,
                       @"sms_type":_sms_type};
    
    [DZNetAPI request:request success:^(id response) {
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1 ) {
            IsSendCode = YES;
            [self submitBtnUsabled];
            JSSHOW(model.msg);
            _sendCodeBtn.userInteractionEnabled = NO;
            _sendCodeBtn.textColor = [UIColor lightGrayColor];
            _sendCodeBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
            _sendCodeBtn.text = [NSString stringWithFormat:@"%lds",timerCount--];
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCountDown) userInfo:nil repeats:YES];
        }else{
            JSSHOW(model.msg);
        }
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

- (void)timerCountDown {
    if (timerCount == 0) {
        [timer invalidate];
        timer = nil;
        _sendCodeBtn.userInteractionEnabled = YES;
        _sendCodeBtn.textColor = BLUE_COLOR;
        _sendCodeBtn.layer.borderColor = BLUE_COLOR.CGColor;
        _sendCodeBtn.text = @"重新获取";
        timerCount = timeLine;
        return;
    }
    _sendCodeBtn.text = [NSString stringWithFormat:@"%lds",timerCount--];
}

- (IBAction)submitClick:(id)sender {
    JSSHOWHUD;
    DZRequestUtil *reqeust = [[DZRequestUtil alloc] init];
    reqeust.requestUrl = @"Index/resetPwd";
    reqeust.params = @{@"mobile":_telStr,
                       @"pwd":_recentPwd.text ,
                       @"verify_code":_vertificationCode.text,
                       @"sms_type":_sms_type};
    [DZNetAPI request:reqeust success:^(id response) {
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            JSSHOW(model.msg);
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            JSSHOW(model.msg);
        }
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

- (void)submitBtnUsabled {
    if (_recentPwd.text.length && _vertificationCode.text.length && IsSendCode) {
        _submitBtn.userInteractionEnabled = YES;
        _submitBtn.backgroundColor = BLUE_COLOR;
    } else {
        _submitBtn.userInteractionEnabled = NO;
        _submitBtn.backgroundColor = UNUSEABLECOLOR;
    }
}

- (IBAction)pwdChanged:(id)sender {
    [self submitBtnUsabled];
}

- (IBAction)msgCodeChanged:(id)sender {
    [self submitBtnUsabled];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [timer invalidate];
    timer = nil;
}

@end
